package com.tanhua.server.controller;

import com.tanhua.commons.utils.UserHolder;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementsController {
    @Autowired
    private MovementsService movementsService;

    @Autowired
    private CommentsService commentsService;

    @Autowired
    private UserFreezeService userFreezeService;

    @PostMapping//发布动态
    public ResponseEntity movements(Movement movement, MultipartFile imageContent[]) throws IOException {
        Long userId = UserHolder.getUserId();
        userFreezeService.checkUserStatus("3",userId);
        movementsService.publishMovement(movement, imageContent);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/all")//查看我的动态
    public ResponseEntity findAllMovement(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize,
            Long userId) {
        PageResult vo = movementsService.findByIdMovements(page, pagesize, userId);
        return ResponseEntity.ok(vo);
    }

    @GetMapping//查看好友动态
    public ResponseEntity findFriendMovement(@RequestParam(defaultValue = "1") Integer page,
                                             @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementsService.findByFriendMovent(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查询推荐动态
     * 1，redis查询推荐动态id列表
     * 2，判断是否有推荐数据（如果不存在返回10条随机数据）
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementsService.findRecommendMovements(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    @GetMapping("/{id}")//查看个人动态
    public ResponseEntity findById(@PathVariable("id") String movementId) {
        MovementsVo vo = movementsService.findById(movementId);
        return ResponseEntity.ok(vo);
    }


    @GetMapping("/{id}/like")//点赞
    //传过来的是动态id
    public ResponseEntity like(@PathVariable("id") String movementId) {
        Integer counts = commentsService.likeComments(movementId);
        //返回喜欢总数
        return ResponseEntity.ok(counts);
    }

    @GetMapping("/{id}/dislike")//取消点赞
    //返回喜欢总数
    public ResponseEntity unlike(@PathVariable("id") String movementId) {
        Integer counts = commentsService.unlikeComments(movementId);
        return ResponseEntity.ok(counts);
    }

    @GetMapping("/{id}/love")//喜欢
    public ResponseEntity love(@PathVariable("id") String movementId) {
        Integer counts = commentsService.loveComments(movementId);
        return ResponseEntity.ok(counts);
    }

    @GetMapping("/{id}/unlove")//取消喜欢
    public ResponseEntity unlove(@PathVariable("id") String movementId) {
        Integer counts = commentsService.unloveComments(movementId);
        return ResponseEntity.ok(counts);
    }

    @GetMapping("/visitors")
    public ResponseEntity visitors(){
        List<VisitorsVo> vo=movementsService.findByVisitors();
        return ResponseEntity.ok(vo);
    }

}
